home *** CD-ROM | disk | FTP | other *** search
/ Giga Games 1 / Giga Games.iso / net / go / comp / mfg < prev    next >
Encoding:
Text File  |  1993-06-20  |  22.3 KB  |  533 lines

  1.     Knowledge Representation in The Many Faces of Go
  2.  
  3.             David Fotland
  4.  
  5.             4863 Capistrano Ave
  6.             San Jose Ca 95129
  7.             fotland@hpihoc.cup.hp.com
  8.  
  9.             February 27, 1993
  10.  
  11.  
  12. Abstract:
  13.  
  14. This paper describes the representations of Go knowledge in The Many
  15. Faces of GO, a strong computer Go program.  Knowledge is encoded in the
  16. algorithms that recognize low level features, in the data structures
  17. that describe the current position, and in patterns that are used to
  18. suggest plausible moves.
  19.  
  20. 1. Introduction
  21.  
  22. The Many Faces of Go is one of the strongest Go programs commercially
  23. available.  It won the United States Computer Go competitions in
  24. 1988, 1991, and 1992, and is ranked 13 kyu in the USA.  It is based on
  25. traditional AI techniques, such as alpha-beta search, rule based expert
  26. systems, and pattern matching.  The go playing algorithm is about 30K
  27. lines of 'C', plus a pattern database and a joseki database, and was
  28. written by one person, part time, over a 10 year period.  As a
  29. commercial program, it has about another 20K lines of code in user
  30. interfaces for the IBM-PC, X windows, and PenPoint.
  31.  
  32. Go is a much more difficult AI problem than Chess, and must be attacked
  33. with a knowledge intensive approach rather than brute force search.
  34.  
  35. 2. Go is harder than Chess
  36.  
  37. There are several reasons why Go is a more difficult problem than Chess.
  38. Strong chess programs depend on full width search 7 or more moves deep.
  39. This is not possible in Go since a Go position is much more difficult to
  40. evaluate than a chess position.  An accurate evaluation of a Go position must do
  41. life and death analysis and assign a value for relative thickness and territory.  Just
  42. Determining the location of secure territory is a difficult problem.  A chess
  43. evaluation is dominated by the values of the pieces remaining on the board,
  44. which is easy to get right.  If a go program misreads a life and death fight
  45. there would be a large error in the evaluation score.  This leads to an
  46. evaluation function for a go program that is orders of magnitude slower than
  47. the chess evaluation function.  Where a PC chess program can examine 100,000
  48. positions each move, Many Faces examines less than 100 positions before
  49. deciding on a move.
  50.  
  51. Second, Go has many more legal plays than Chess at each move.  Even if the
  52. evaluation functions were the same speed, the go program could not look as
  53. many moves ahead.
  54.  
  55. Third, people read deeper in Go than in Chess.  A strong chess player might
  56. read 10 moves ahead, but a strong Go player routinely reads much deeper.
  57. This is because the board does not change as much after a move in go as it
  58. does in chess, so it is easier for a person to visualize the board many moves
  59. ahead.  Even a beginner can read 60 moves deep in a ladder that crosses the
  60. board.
  61.  
  62. 3. Knowledge in The Many Faces of Go
  63.  
  64. Go knowledge is incorporated into Many Faces of Go in 5 major areas.
  65. First, there is low level go knowledge built into the evaluation, and
  66. hard coded in 'C' 'if' statements.  This is knowledge used to determine
  67. connectivity, eyes, potential eyes, and territory, as well as the move
  68. generators and move sorters used by the tactician.  The general
  69. philosophy here is to classify first, then analyze.  This makes the code
  70. easier to debug.
  71.  
  72. Second, there is the dynamic knowledge stored about the state of the
  73. current position.  This data is stored in a global database, and is
  74. built up in successive passes, from low levels to high levels of abstraction.
  75.  
  76. Third, there is a rule based expert system with about 200 rules that
  77. suggests plausible moves for full board evaluation.  I try to only
  78. suggest reasonable moves for evaluation, to reduce the likelihood that
  79. errors in the evaluation function will cause the program to play
  80. terrible moves.
  81.  
  82. Fourth, there is a Joseki database of standard corner patterns,
  83. currently consisting of over 36,000 moves, including all moves from most
  84. of the joseki books published by Ishi Press.
  85.  
  86. Fifth, there is a pattern database of 8x8 patterns, each with a move
  87. tree attached, currently with over 700 patterns and over 4,000 moves.
  88.  
  89. 4. Program Components
  90.  
  91. The dynamic data stored about a position falls into 3 levels.  First is
  92. incremental data, used for low level local data, such as 
  93. point_empty_neighbors, or string_liberties.  These data structures are 
  94. modified incrementally and adjusted as each stone is added to or removed
  95. from the board.  Second, there is data that is recalculated as needed.  After a
  96. move or sequence of moves, the area of the board affected by the move(s)
  97. is recalculated, and areas that are not effected maintain their old
  98. values.  This is used for connection strength, eyes, and string tactics.
  99. Third, there is data that is recalculated for the entire board after a move or
  100. sequence of moves.  These include group strength and radiated influence.
  101.  
  102. The dynamic data consists of:
  103.  
  104. Point environment
  105.     which string is this point part of (or None)
  106.     lists of adjacent Black and White strings
  107.     list of adjacent empty points
  108.     number of adjacent Black, White, and empty points
  109.     list of connections
  110.     which eye is this point part of
  111.     White and Black influence
  112.     ...
  113. String data
  114.     color
  115.     list of points (stones) in string
  116.     number of liberties 
  117.     list of liberty points of string
  118.     list of adjacent enemy strings
  119.     tactical status (captured, threatened, OK)
  120.     list of connections
  121.     which group is this string part of
  122.     ...
  123. Connection data
  124.     two strings
  125.     lists of points in connection
  126.     number of connections
  127.     type of connection
  128.     strength of connection
  129. Eye data
  130.     list of points in eye
  131.     list of vital points
  132.     type of eye
  133.     number of eyes
  134. Group data
  135.     list of strings in group
  136.     list of eyes in group
  137.     number of eyes in group
  138.     group strength
  139.     list of potential eyes
  140.     lists of running points
  141.     list of adjacent enemy groups
  142. Territory
  143. Score
  144.  
  145. Incremental data structures are modified as moves are made and taken
  146. back.  Other data structures are maintained by the evaluation function.
  147.  
  148. EVALUATION FUNCTION
  149.  
  150. I have described the evaluation function elsewhere.  Its major
  151. components are the tactical analyzer, which reads a single string with
  152. the twin goals of removing it from the board, or making 5 liberties or
  153. two eyes; the connection evaluator; the eye evaluator; the group
  154. strength evaluator; and the territory evaluator.  It assigns a score to
  155. the position, using Chinese style counting, where each point on the
  156. board can have a value between +1 and -1, depending on how strongly it
  157. is controlled by white or black.  Evaluation is a multiple pass process.
  158.  
  159. STRATEGY
  160.  
  161. Before each move, a strategy function looks at the dynamic data and
  162. attempts to focus attention on the important areas of the board.  It
  163. decides what phase the game is in.  Fuseki lasts as long as there are
  164. corners that still contain unplayed joseki moves.  The game is in
  165. Endgame if more that 120 moves have been played and all groups are
  166. stable.  Otherwise the game is in the Middle Game.  It is possible for
  167. the phase of the game to go back and forth from endgame to middle game.
  168. The phase of the game is used as a qualifier for some of the move
  169. generation moves.  For example the fill_dame rule will not fire unless
  170. the phase is the endgame.
  171.  
  172. Strategy determines the relative score, and classifies it as "way
  173. ahead" (over 40 points), "ahead" (20-40 points), "about even", "behind" 
  174. (over 10 points), and "way behind (over 20 points)".  This is also used
  175. to qualify move generation rules.  For example if the program is way
  176. ahead, it will play extra moves to be certain of big captures.  If
  177. it is way behind it will try unsound invasions.
  178.  
  179. The strategy function determines the value of taking sente.  This declines
  180. from 7 points at the start of the game to 1 point at move 220 and above.  At
  181. the end of a full board lookahead and quiescence search, this value is added
  182. to the side with sente, since presumably it can get that many points with a 
  183. big move somewhere.  Seven points is used at the start of the game since 
  184. internally the program uses Chinese style counting, which increases the value
  185. of a move by one point relative to Japanese counting, and the value of the
  186. first move is known to be about 5 1/2 points with Japanese counting.
  187.  
  188. It also finds friendly groups that urgently need defending.  These are
  189. cutting groups and big groups.
  190.  
  191. It finds enemy groups that urgently need capturing.  These are big
  192. groups, cutting groups, and groups adjacent to groups that urgently need
  193. defending.
  194.  
  195. Attack and defense of groups marked urgent, and moves marked urgent by
  196. the pattern matcher, are examined first, and if a reasonable urgent move
  197. is discovered, no other moves are considered.
  198.  
  199. FULL BOARD LOOKAHEAD
  200.  
  201. Sequences for full board lookahead are provided directly from the Joseki
  202. and Pattern databases.  Joseki variations are laid out on the board and
  203. evaluated at the endpoints.  This allows the program to pick joseki that
  204. it understands and that work well with adjacent corners.  Patterns also
  205. suggest move sequences that are evaluated at the endpoints.
  206.  
  207. Move generators examine the group types, eye potentials, and running
  208. points to generate move sequences to kill or save groups, fight semeai,
  209. and run out to the center.
  210.  
  211. A rule based system generates other reasonable moves, such as
  212. extensions, moyo expansion, etc.
  213.  
  214.  
  215. QUIESCENCE SEARCH
  216.  
  217. Since the evaluation function ultimately reduces the position to a
  218. single number, the score in 50ths of points, it is only accurate in a
  219. quiet position.  It generates moves to save unsettled friendly groups or
  220. kill unfriendly enemy groups, and calls itself recursively until it 
  221. finds a quiet position.  For contact fights, a special set of "obvious
  222. local answer" patterns suggests moves that quiet the position.
  223.  
  224. 5. Dynamic Data
  225.  
  226. CONNECTIONS
  227.  
  228. Each connection structure records all connectivity between a pair of
  229. strings.  A connection point is a liberty of one string that sees a
  230. stone of another string along a straight line, either 1, 2, or 3 points
  231. away.  This suffices to discover hane, one point jump, knight moves, 2
  232. point jumps, large knight moves, and 3 points jumps.  It does not see
  233. double kosumi or watari played on the 1st line from two stones on the
  234. second line.  These are handled by special case patterns.  A low level
  235. incremental data structure that records the distance to the nearest
  236. stone in each direction at each point makes it easy to incrementally
  237. maintain the basic connections.  The structure looks like:
  238.  
  239. string s1, s2;        // two string numbers
  240. int num_d1;        // number of distance one connections
  241. int num_d2;        // number of distance 2 connections
  242. int num_d3;        // number of distance 3 connections
  243. list_t d1points;    // list of distance one connection points
  244. list_t d2points;    // list of distance 2 connection points
  245. list_t d3points;    // list of distance 3 connections
  246. int type;        // type of connection:
  247.                 // hane
  248.                 // one point jump
  249.                 // half knight's move
  250.                 // knight's move
  251.                 // two point jump
  252.                 // half large knight's move
  253.                 // large knight's move
  254.                 // 3 point jump
  255.                 // multiple
  256. int strength;        // strength of the connection
  257.                 // Already cut
  258.                 // Cuttable
  259.                 // Shared connection
  260.                 // Connected with aji
  261.                 // Connected solidly
  262.  
  263. Type and strength are recalculated as needed.  The others are incremental.
  264. Each point maintains a list of connection structures that it appears in,
  265. and each string maintains a list of connection structures that it
  266. appears in.  The connection analysis code (representing low level go
  267. knowledge coded directly in 'C') is about 1500 lines.
  268.  
  269. EYES
  270.  
  271. Each eye structure records information about a single block of empty
  272. points completely or partially surrounded by stones of the same color,
  273. or a tactically captured string.
  274.  
  275. list_t points;    // empty points in the eye
  276. list_t vital;    // vital points for the eye, destroy it or finish it
  277. int type;    // type of the eye
  278.             // single point
  279.             // two point
  280.             // line eye (closed, open one end, open both ends)
  281.             // big eye
  282.             // dead group eye
  283. int color;    // color of the eye
  284. int min_eyes;    // number of eyes if opponent moves twice
  285. int typ_eyes;    // number of eyes if opponent moves first
  286. int max_eyes;    // number of eyes if I move first
  287.  
  288. The number of eyes is stored using the value 8 for one eye, 4 for 1/2
  289. eye, etc.  Each point can be part of only one eye, and which eye is
  290. recorded with the other data about a point.  All parts of the eye
  291. structure are recalculated as needed.  Eye analysis is about 2400 lines
  292. of 'C'.  There are a lot more patterns to recognize for eyes than for
  293. connections, and nothing is incremental, so if I had to do it over again
  294. I would make it pattern based rather than coded in 'C'.
  295.  
  296. POTENTIAL EYES
  297.  
  298. Each group has a list of potential eye structures, and lists of running
  299. points.  These are used to evaluate group strength and to generate
  300. attacking/defending moves.  A potential eye contains a value, location,
  301. and type.  Types are:
  302.  
  303. Extend along the edge 
  304.     value is number of new points of eye space
  305.     location is liberty to extend from
  306. Play vital point to make an eye 
  307.     value is number of new eyes
  308.     location is vital point
  309. Connect to another group 
  310.     value is number of eyes
  311.     location is connection number
  312. Capture tactically threatened string
  313.     value is number of eyes
  314.     location is string number
  315. Defend undercut territory on the edge
  316.     value is number of new points for eye space
  317.     location is liberty being undercut
  318.  
  319. Running points are recorded in several lists, by type, depending on
  320. whether running this direction is toward friendly or enemy stones, etc.
  321.  
  322. Potential eyes and running points are reevaluated on the whole board at
  323. each evaluation, consuming about 7% of the program's time.
  324.  
  325. INFLUENCE FUNCTION
  326.  
  327. An influence function is used that radiates from each stone with an
  328. initial value that is dependent on the group strength, and falls off as
  329. one over the distance.  Radiated values are summed independently at each
  330. point for black and white.  Influence does not radiate through a stone
  331. or a connection.  The influence is used to calculate territory and
  332. thickness, find the best gradient for running moves, and help generate
  333. moyo building or reducing/invading moves.  Influence is not used to
  334. determine group connectivity.
  335.  
  336. A function that falls off as 1/distance will create a constant field inside
  337. any fully enclosed area, no matter how big it is, which is desirable
  338. for estimating territory.  Dead stones radiate negative influence.
  339.  
  340. 6. Rule Based System For Move Suggestion
  341.  
  342. The move suggestion experts are:
  343.  
  344. Fuseki
  345.     Playing in empty corner
  346.     Shimari and kakari
  347.     Joseki moves
  348. Big moves on edge
  349.     Towards enemy
  350.     Invasions
  351.     Between friendly stones
  352. Playing in the center (reducing moves and junction lines)
  353. Playing safe when ahead
  354.     Respond when enemy adds stone to dead group
  355.     Add extra stone to kill big group that is probably dead
  356. Squirming around when behind
  357.     Make a bogus invasion
  358.     Try to make a hopeless group live
  359. Pattern matching
  360.     patterns for cutting and connecting
  361.     patterns for surrounding and avoiding getting surrounded
  362.     patterns for invasion and defending against invasion
  363.     endgame patterns
  364.     killing/saving groups patterns
  365.     Shape moves
  366. Saving a weak group
  367.     making eyes
  368.     running
  369.     fighting semeais
  370. Save threatened string
  371. Capture threatened string
  372. Killing a weak group
  373.     surrounding
  374.     taking eyes
  375. Cutting and connecting
  376. Contact fights
  377.     Blocking
  378.     extending for liberties
  379.     hane 
  380. Ko threats
  381.     make a big atari
  382. Filling dame
  383.  
  384. The move suggestion code is easily extensible by adding more patterns or
  385. firing rules based on other criteria.  The program has text associated with
  386. each rule so it can "explain" its reasons for making moves.  This makes it
  387. easier to debug.
  388.  
  389. 7. Joseki Database
  390.  
  391. The Joseki database is stored as a directed acyclic graph of moves, with
  392. the root representing an empty corner.  Transpositions are built into the
  393. database manually as data is entered.  An uncompressed representation is
  394. maintained in an ASCII file.  Each uncompressed node contains:
  395.  
  396. X and Y relative to corner (1-16 each)
  397. Sibling pointer
  398. First child pointer
  399. Type: Urgent, normal, complex, trick, bad, ignore, ...
  400. flags: color, symmetric, color reverse
  401.  
  402. Urgent moves take priority over any other
  403. Complex and trick moves are avoided by the computer unless it is behind.
  404. Bad moves are never played by the computer, but are in the database so
  405. it can respond correctly if an opponent plays them.
  406. Ignore is used to delete an erroneous entry in the database.
  407.  
  408. Color indicates whether the current move is the same color or opposite color
  409. as the first move in the corner.  Symmetric indicates that this move
  410. makes the corner symmetric again.  Color reverse is needed for some
  411. transpositions, for example certain 3-4 joseki are 5-3 joseki transposed with
  412. colors reversed.
  413.  
  414. A compressed representation is used directly by the program.
  415. (x,y) are reduced to 6 bits via a lookup table.  Values 0-62 index the
  416. lookup table of the 62 most common x,y pairs.  Value 63 escapes to the
  417. next byte which contains the full x,y pair.  Sibling and child pointers
  418. are replaced by two bits, has_sibling, and has_child.  Data is stored in
  419. depth first order, except where lines converge due to a transposition,
  420. when a full 2 byte pointer is stored.  Most entries are 1 byte long.  The
  421. longest possible entry is 5 bytes.  Across the entire database, the
  422. average move is stored in 1.2 bytes.
  423.  
  424. 8. Pattern Database
  425.  
  426. There is a pattern matcher with over 500 patterns in it.  Each pattern is
  427. 8x8, where each point is specified as black, white, empty, don't care, 
  428. white or empty, black or empty, or black or white.  Each pattern has
  429. a set of attributes with it that must also match.  For example, "the
  430. stone at (4,2) must be alive", or "the stone at (2,5) must have at least
  431. 2 liberties".  Each pattern has a move tree attached that is used for
  432. full board lookahead.
  433.  
  434. Patterns are also used for reading obvious local sequences based on shape.
  435.  
  436. Internally a pattern is stored as three 8 byte arrays, one for black points,
  437. one for white, and one for empty.  For example a black point in the
  438. pattern is indicated by setting the appropriate bit in the black array.
  439. Black or Empty is indicated by setting bits in both the black and empty
  440. arrays.  Don't care is indicated by setting corresponding bits in all 3
  441. arrays.  Each pattern needs to be matched at each point on the board, in
  442. 8 orientations, and with colors reversed.  Rather than rotate the
  443. patterns, I rotate the board before comparing.  Color reversal is handled
  444. by reversing the black and white arrays.  Once I have 3 arrays extracted
  445. from the board for a particular position, the pattern match is simple.
  446. Just bitwise AND the corresponding Black, white, and empty arrays; bitwise
  447. OR the three results, and if the result is all ones, then there is a match.
  448.  
  449. After I find a matching pattern I check that the attributes specified by
  450. the pattern match.
  451.  
  452. I plan to have at least 1000 patterns, so performance is an issue.  1000
  453. patterns compared at 361 locations at 16 orientations is over 5
  454. million comparisons.  A full comparison requires 24 byte ANDS, 16 byte
  455. ORS, and 8 compares, or 48 primitive operations.  That's almost 300
  456. million operations to determine which patterns match at a single board
  457. position.
  458.  
  459. I use an 8 bit hash function to reduce the number of patterns examined.
  460. Of course since there are don't cares in the patterns, a pattern may appear
  461. on more than one hash chain (current average is 3 chains).  The hash
  462. reduces the number of patterns examined by about a factor of 20.  I
  463. remember which patterns match from move to move and only rematch in the
  464. area near the last move.  This reduces the number of points where patterns
  465. need to be matched from 361 to about 50.  I do the bitwise operations 
  466. 32 bits at a time, and most patterns miscompare on the first word, which
  467. reduces the number of logical operations about 6 on average.  This reduces
  468. the number of operations to match all patterns down to about 350,000.
  469. This is still slow enough that I only match all the patterns once
  470. each time the computer has to make a move, to find sequences to read.  A
  471. small subset of the patterns is used for obvious local lookahead during
  472. the quiescence search.  The program currently spends about 1.5% of its time
  473. matching patterns.
  474.  
  475. 9. Knowledge acquisition
  476.  
  477. A graphical joseki editor allows me to enter about 400 new moves an hour,
  478. and compiles the database to the compressed binary form that the program
  479. uses.
  480.  
  481. A graphical pattern editor allows easy modification of the patterns
  482. or move trees and can display 36 patterns at a time.
  483.  
  484. The program can play through a professional game and compare its choices to
  485. the pro's.  When it didn't even consider the pro's move, it cuts out
  486. the pattern from the pro's game and saves it.  This turned out to be much
  487. less useful that I expected, since many of these moves involved difficult
  488. fights, where an 8x8 pattern was not big enough, or where the proper move
  489. was selected based on deep reading, not the local stone pattern.
  490.  
  491.  
  492. 10. Debugging Aids
  493.  
  494. Many of the data structures are incremental.  I can optionally include code 
  495. that periodically recalculates the incremental data structures from scratch
  496. and verifies that they are correct.  This code also walks all of the
  497. lists and finds any memory leaks.  I'm confident that the program contains
  498. no bugs in incremental updates or memory leaks.
  499.  
  500. Some data values are only recalculated as needed.  Sometimes an eye or 
  501. a tactical lookahead needs to be recalculated, and it doesn't get noticed.
  502. To detect bugs here I can include code that evaluates each position twice,
  503. before and after lookahead.  A difference indicates that something wasn't
  504. reevaluated.  There are many situations that can cause missed reevaluations.
  505. In test games they come up about once in 1000 moves.  These are not critical
  506. bugs since they mimic similar oversights that people make, but I fix them
  507. as I find them.
  508.  
  509. The debugging version of the program can dump all of the internal data
  510. structures in an easy to read form.  When the program makes a bad move I
  511. can quickly determine the cause.
  512.  
  513. I run two kinds of regression tests.  First, I run about 100 self play games
  514. at different levels and handicaps with all the checking code included.
  515. Second, I have about 400 problems from Graded Go Problems For Beginners,
  516. and the program can automatically play thru them to produce a report of
  517. which problems got the wrong answers.  
  518.  
  519. I wrote an interface between Many Faces of Go and the Internet Go Server
  520. so Many Faces can play games there unattended.  I collect some of these
  521. test games as samples that I can examine in detail to fix the problems
  522. that lead to bad moves.
  523.  
  524. 11. Summary
  525.  
  526. Playing the game of Go well is a difficult AI problem.  Because of the
  527. large board and difficult evaluation, large searches are impractical,
  528. and a knowledge intensive approach is required.  Many Faces encodes
  529. local, low level knowledge in C algorithms for speed, and higher level
  530. pattern knowledge in databases optimized for size.
  531.  
  532.  
  533.